package com.emeraldparser.collect;

import com.google.common.base.Objects;
import com.google.common.collect.Multiset;

public abstract class AbstractMultisetEntry<E> implements Multiset.Entry<E> {

	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		
		if (o == null) {
			return false;
		}
		
		if (!(o instanceof Multiset.Entry<?>)) {
			return false;
		}
		
		Multiset.Entry<?> other = (Multiset.Entry<?>) o;
		
		return Objects.equal(this.getElement(), other.getElement())
	        && this.getCount() == other.getCount();
	}

	@Override
	public int hashCode() {
		E element = getElement();
		int count = getCount();
		
		return ((element == null) ? 0 : element.hashCode()) ^ count;
	}

	@Override
	public String toString() {
		E element = getElement();
		int count = getCount();
		
		if (count == 1) {
			return String.valueOf(element);
		} else {
			return element + " x " + count;
		}
	}

}
